<--- %%NOBANNER%% --> bmark.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Update the contents of an oldbmark with either a variable from a   |
| data set or a 'text', then update the oldbmark name to newbmark    |
| name; Or just to create a bookmark;                                |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Argument:                                                          |
|    oldbmark: the name of the bookmark you want to update;          |
|    newbmark: the name of the bookmark after you updated it;        |
|    var: the name of the variable you want to use to update the     |
|         bookmark;                                                  |
|    format: the format for the value of variable you want to update |
|         the bookmark;                                              |
|    color: the color of the text;                                   |
|    indata: the name of the dataset you want to use;                |
|    wordref: word reference; not necessary default is "wordsys";    |
| Note:                                                              |
| if oldbmark not equal empty, then                                  |
|       go to the oldbmark;                                          |
|       if newbmark is given, then update the oldbmark name to the   |
|       name; new one; else (new book mark isn't given), keep the    |
|       oldbmark;                                                    |
| else (oldbmark is empty), if a new bookmark is given then create a |
|       new bookmark;                                                |
| if there is a data set then update the contents of the oldbmark to |
|        the variable value in the data set;                         |
| else if the data set is empty thenupdate the contents of the       |
|        oldbmark to the text input by var;                          |
| Color: what color do you want the text be displayed;               |
|        By default, it will keep the current color;                 |
|------------<-- Start of Files or Arguments Needed-->---------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %bmark('b1',data1,x,format=5.0,path='c:\temp.doc');       |
| Usage:  %bmark(bookmark, data, var,  format=, path=);              |
\-------------------<-- End of Files Created-->---------------------*/
%macro bmark(bookmark, data, var,  format=, path=&__lastpath);
%global __lastpath;
%if (%quote(&path) ne) %then %do; 
   %let __lastpath=%trim(%left(%sysfunc(dequote(&path)))); 
%end;
%else %if (%quote(&path) eq) and (%quote(&__lastpath) eq) %then %do;
   %put ==> Alert! You must provide a valid file path in order to update a bookmark.;
   %goto finish;
%end;
%let bookmark=%trim(%left(%sysfunc(dequote(&bookmark))));
%let __varnum=0; %let __vartype=; %let __dsid=0; %let __varnumstr=0; 
%if (%quote(&data) ne) %then %do;
   %if (%sysfunc(exist(&data))) %then %do;
      %let __dsid=%sysfunc(open(&data));
      %let __varnumstr=%sysfunc(VARNUM(&__dsid,__str));
      %let __varnum=%sysfunc(VARNUM(&__dsid,&var));
      %let __vartype=%sysfunc(VARTYPE(&__dsid, &__varnum));
      %let __rc=%sysfunc(close(&__dsid));
   %end; 
%end; 
%if (&__varnum) or (%index(%nrbquote(&var), %nrbquote(%sysfunc(dequote(%quote(&var))))) > 1) %then %do;
   %if (%sysfunc(fileexist(&__lastpath))) %then %do;
      Filename __path dde "Winword|%trim(%left(%sysfunc(dequote(&__lastpath))))!&bookmark" notab lrecl=1048576;
      data _null_; 
         file __path; 
         %if (%quote(&format) ne) and (%quote(%upcase(&__vartype)) eq %quote(C)) %then %do;
         format &var &format;
         %end;
         %if (%sysfunc(exist(&data))) %then %do;
         set &data %if (&__varnumstr) %then %do; (drop=__str) %end;; 
         %end;
         %if (%quote(%upcase(&__vartype)) eq %quote(N)) and (%quote(&format) ne) %then %do;
            __str=trimn(left(put(&var, &format)));
         %end;
         %else %if (%quote(%upcase(&__vartype)) eq %quote(N)) and (%quote(&format) eq) %then %do;
            __str=trimn(left(put(&var, best12.)));
         %end;
         %else %if (%quote(%upcase(&__vartype)) eq %quote(C)) %then %do;
            __str=trimn(left(&var));
         %end;
         %else %if (%index(%nrbquote(&var), %nrbquote(%sysfunc(dequote(%quote(&var))))) > 1) %then %do;
            __str=trim(left(&var));
         %end;
         put __str;
      run;
   %end;
   %else %do; %put ==> Alert! File path  is not valid.; %goto finish; %end;
%end;
%else %put ==> Alert! Dataset &data does not have variable named "&var"!;
%finish:
%mend bmark;